package com.zq.ht.web.controller.content;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zq.ht.common.util.DataGrid;
import com.zq.ht.common.util.FileRWUtil;
import com.zq.ht.common.util.PageUtil;
import com.zq.ht.common.util.Result;
import com.zq.ht.model.TNews;
import com.zq.ht.model.TProduct;
import com.zq.ht.web.service.content.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;

/**
 * Created by admin on 2016/6/26.
 */
@Controller
public class ProductController {
    @Autowired
    private ProductService productService;

    @RequestMapping(value = "product_page")
    public String newsPage(){
        return "product/productInfo";
    }

    @RequestMapping(value = "product_page_info")
    @ResponseBody
    public String productInfo(HttpServletRequest request,int type){
        DataGrid grid = new DataGrid();
        try {
            PageUtil pageUtil = new PageUtil(request);
            pageUtil = productService.getProductList(pageUtil,type);
            grid.setRows(pageUtil.getResults());
            grid.setTotal(pageUtil.getTotalRows());
        }catch (Exception e){
            e.printStackTrace();
        }
        return JSONObject.toJSONString(grid);
    }

    @RequestMapping(value = "product_save")
    public @ResponseBody String newsSave(TProduct product,MultipartHttpServletRequest request){
        Result result = new Result();
        try{
            productService.saveProduct(product,request);
            result.setSuccess(true);
            result.setMsg("新闻保存成功。");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMsg("新闻保存成功。");
            e.printStackTrace();
        }
        return JSON.toJSONString(result);
    }

    @RequestMapping(value = "product_del")
    public @ResponseBody String newsDel(int id){
        Result result = new Result();
        try{
            productService.deleteProduct(id);
            result.setSuccess(true);
            result.setMsg("删除成功。");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMsg("删除失败。");
            e.printStackTrace();
        }
        return JSON.toJSONString(result);
    }

}
